package com.huaqin.obeacons;

import cn.bmob.v3.listener.SaveListener;

import com.huaqin.obeacons.bmob.LogOutData;
import com.huaqin.obeacons.bmob.SignInData;
import com.huaqin.obeacons.utils.SharedPreferenceUtil;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class LogOutActivity extends Activity {
	private final String TAG = "LogOutActivity";
	private ImageButton backHomeView;
	private EditText requestInfoView;
	private Button logoutBtnView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_logout);
		backHomeView = (ImageButton) findViewById(R.id.btn_home);
		backHomeView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		requestInfoView = (EditText) findViewById(R.id.request_content);
		logoutBtnView = (Button) findViewById(R.id.btn_logout);
		logoutBtnView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String requstText = requestInfoView.getText().toString();
				Log.i(TAG, "request: " + requstText);
				if (requstText.isEmpty() || requstText.trim().length() == 0) {
					Toast.makeText(getApplicationContext(),
							"申请原因不能为空，请重新输入，谢谢！", Toast.LENGTH_SHORT).show();
				} else {
					submitRequest(getApplicationContext(), requstText);
				}
			}
		});
	}

	private void submitRequest(Context context, String cause) {
		// add logout data for bmob begin
		LogOutData mLogOutData = new LogOutData();
		if (!SharedPreferenceUtil.accountIsEmpty(context)) {
			String account = SharedPreferenceUtil.getAccount(context);
			String name = SharedPreferenceUtil.getName(context);
			mLogOutData.setAccount(account);
			mLogOutData.setName(name);
			mLogOutData.setCause(cause);
			mLogOutData.setState("SUBMIT");
			mLogOutData.save(context, new SaveListener() {

				@Override
				public void onSuccess() {
					// TODO Auto-generated method stub
					Log.d(TAG, "mLogOutData save onSuccess");
					Toast.makeText(getApplicationContext(),
							"申请提交成功，我们的IT人员会尽快处理，谢谢！", Toast.LENGTH_SHORT)
							.show();
				}

				@Override
				public void onFailure(int arg0, String arg1) {
					// TODO Auto-generated method stub
					Log.d(TAG, "mLogOutData save onFailure arg1:" + arg1);
					if (arg1.startsWith("The network is not normal")) {
						Toast.makeText(getApplicationContext(),
								getResources().getString(R.string.network_fail),
								Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
		// add logout data for bmob end
	}
	
}
